package com.mgq.juc.cas;


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicMarkableReference;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * AtomicReference 无法解决A-B-A问题
 * AtomicMarkableReference 有时候我们只想关注是否发生了变化,不想使用版本,就可以使用AtomicMarkableReference
 * AtomicMarkableReference相当于是AtomicStampedReference的简化版
 */
@Slf4j
public class TestAtomicMarkableReference {
    //初始stamp为0
    static AtomicMarkableReference<String> stampedReference = new AtomicMarkableReference<>("A",true);

    public static void main(String[] args) throws InterruptedException {
        change();
        Thread.sleep(1000);
        boolean b = stampedReference.compareAndSet("A", "C", true,false);
        log.debug(String.valueOf(b));

    }

    private static void change() {
        new Thread(()->{
            boolean b = stampedReference.compareAndSet("A", "B", true,false);
            log.debug(String.valueOf(b));
        },"t1").start();

    }
}
